function getParameter(param)  
{  
    var query = window.location.search;//获取URL地址中？后的所有字符  
    query = query.substring(1);
    if ( query == null || query == '' ) {
        return null;
    }

    var parameterList = query.split('&');
    for ( parameterIndex in parameterList ) {
        var parameter = parameterList[parameterIndex];

        var keyValue = parameter.split('=');
        var key = keyValue[0];
        var value = keyValue[1];

        if ( key == param ) {
            return value;
        }
    }

    return null;
}  

function makeParameters(parameters) {
    parameterList = [];
    for ( var key in parameters ) {
        var value = parameters[key];
        
        parameterList.push(key + '=' + value);
    }

    return parameterList.join('&');
}

function getParameters(param)  
{  
    var query = window.location.search;//获取URL地址中？后的所有字符  
    query = query.substring(1);
    if ( query == null || query == '' ) {
        return null;
    }

    var parameterList = query.split('&');
    var parameters = {};
    for ( parameterIndex in parameterList ) {
        var parameter = parameterList[parameterIndex];

        var keyValue = parameter.split('=');
        var key = keyValue[0];
        var value = keyValue[1];

        parameters[key] = value;
    }

    return parameters;
}  

function postJSON(url, data, callback, error) {
   $.ajax({ 
        url: url,
        type: 'post',
        contentType: 'application/json',
        data: JSON.stringify(data),
        dataType: 'json',
        success: function(data) {
            callback(data);
        },
        error: function(req, message, e) {
            console.log(message);

            if ( error ) {
                error(req, message, e);
            }
        }
    });
}

var AGE_FILTERS = {
    1: {
        min: 20,
        max: 30
    },
    2: {
        min: 30,
        max: 40
    },
    3: {
        min: 40,
        max: 200
    }
};
var WORKING_YEARS_FILTERS = {
    1: {
        min: 1,
        max: 5
    },
    2: {
        min: 5,
        max: 10
    },
    3: {
        min: 10,
        max: 200
    }
};

var caregivers = {};

$(document).ready(function() {
    var parameters = getParameters();
    var age = parameters["age"];  
    var workingYears = parameters["working_years"];  
    var educationBackground = parameters["edu_bkgd"];  


    age = Number(age);
    workingYears = Number(workingYears);
    educationBackground = Number(educationBackground);

    if ( isNaN(age) || age > 3 ) {
        age = 0;
    }
    if ( isNaN(workingYears) || workingYears > 3 ) {
        workingYears = 0;
    }
    if ( isNaN(educationBackground) || educationBackground > 4 ) {
        educationBackground = 0;
    }

    searchCaregivers();

	$('#giver-content-right').delegate('.collect', 'click', function(event) {
		event.preventDefault();
		
		var giverInfo = $(this).parents('.giver-info');
		var caregiverId = Number($(giverInfo).attr('data-caregiver-id'));
	    
        var userId = caregivers[caregiverId].user.id;

        alert('护工id: ' + caregiverId + '\n用户id: ' + userId);
	});

    $('#select-age li').click(function(e) {
        age = Number($(this).attr('data-index'));

        searchCaregivers(age, workingYears, educationBackground);
    });
    $('#select-experience li').click(function(e) {
        workingYears = Number($(this).attr('data-index'));

        searchCaregivers(age, workingYears, educationBackground);
    });
    $('#select-schooling li').click(function(e) {
        educationBackground = Number($(this).attr('data-index'));

        searchCaregivers(age, workingYears, educationBackground);
    });

    function searchCaregivers() {
        var filters = {};

        if ( age != 0 ) {
            filters.age = AGE_FILTERS[age];
        }
        if ( workingYears != 0 ) {
            filters.workingYears = WORKING_YEARS_FILTERS[workingYears];
        }
        if ( educationBackground != 0 ) {
            filters.educationBackground = Number(educationBackground);
        }

        postJSON('/caregiver/search', {
                filters: filters
            },
            function(data) {
                if ( !data.successful ) {
                    console.log(data);
                    return;
                }

                $('#giver-content-right').html('');
                data.data.caregivers.forEach(function(caregiver) {
                    caregivers[caregiver.id] = caregiver;
                    var html = '<div class="giver-info" data-caregiver-id="' + caregiver.id + '">' + 
                        '<div class="giver-image-box"><img src="/upload/' + caregiver.photo_url + '" alt=""></div>' +
                        '<div class="text-intro-box">' +
                            '<div class="giver-name-age"><span>' + caregiver.user.name + 
                                '</span><span>' + caregiver.user.age + '</span>岁</div>' +
                            '<div class="working-experience">' +
                                caregiver.working_expierences +
                            '</div>' +
                            '<div class="giver-address">' +
                                '住址：<span class="address">' + caregiver.user.address + '</span>' +
                            '</div>' +
                        '</div>' +
                        '<span class="collect btn-collect">收藏</span>' +
                        '<img class="collect" src="../img/favorite.png" alt="">' +
                    '</div>';

                    $('#giver-content-right').append(html);
                });
            }
        );

        refreshSelection();
    }

    function refreshSelection() {
        $('#select-experience li').removeClass('active');
        $('#select-age li').removeClass('active');
        $('#select-schooling li').removeClass('active');

        $('#select-age li[data-index="' + age + '"]').addClass('active');
        $('#select-experience li[data-index="' + workingYears + '"]').addClass('active');
        $('#select-schooling li[data-index="' + educationBackground + '"]').addClass('active');
    }
});
